var $ = require('../../common/helper.js');
Page({
	data: {
		keyword: '',
		data: [],
		origin_data: []
	},
	onLoad: function (options) {
		var _this = this;
		$.get('api.php?app=cart&act=city&province_id=' + options.province_id, function (json) {
			_this.setData({
				data: json.data,
				origin_data: json.data
			});
		});
	},
	search: function () {
		var data = this.data.origin_data, keyword = this.data.keyword, new_data = [];
		if (!keyword.length) {
			new_data = data;
		} else {
			for (var i = 0; i < data.length; i++) {
				if (data[i].name.indexOf(keyword) > -1) new_data.push(data[i]);
			}
		}
		this.setData({
			data: new_data
		});
	},
	keyword: function (e) {
		var keyword = e.detail.value;
		this.setData({
			keyword: keyword
		});
	},
	select: function (e) {
		var data = e.currentTarget.dataset,
			apps = $.getApps(), app = apps[apps.length - 3];
		data.name = data.province+' '+data.city;
		app.setParam('city', data);
		wx.navigateBack({
			delta: 2
		});
	}
})